﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ivi.Visa.Interop;
using System.Data;

namespace SensitivityTest.model
{
    class SMC100A
    {
         private  FormattedIO488 instrumentSig1 = new FormattedIO488();//488接口
         private FormattedIO488 instrumentSig2 = new FormattedIO488();//488接口

         private  ResourceManager rm1 = new ResourceManager();
         private ResourceManager rm2 = new ResourceManager();


         public Boolean SigInit(String sig1,String sig2, out string ErrStr)
        {
            try
            {
                instrumentSig1.IO = (IMessage)rm1.Open(sig1, AccessMode.NO_LOCK, 2000);
                instrumentSig1.IO.Timeout = 3000;
                instrumentSig1.WriteString("*rst");
                ErrStr = "信号源1连接成功";
                //return true;
            }
            catch (Exception e)
            {
                ErrStr = "信号源1连接失败" + e.Source + e.Message;  
                return false;
            }

            try
            {
                instrumentSig2.IO = (IMessage)rm2.Open(sig2, AccessMode.NO_LOCK, 2000);
                instrumentSig2.IO.Timeout = 3000;
                instrumentSig2.WriteString("*rst");
                ErrStr = "信号源2连接成功";
                //return true;
            }
            catch (Exception ex)
            {
                ErrStr = "信号源2连接失败" + ex.Source + ex.Message;
                return false;
            }
            return true;
        }

         public void InitFreqPow_Sig(int num,Frm_Main fmain)
         {
             if (num == 1)
             {
                 instrumentSig1.WriteString(":SOUR:FREQ " + fmain.dt.Rows[0]["F1"] + "MHz");
                 instrumentSig1.WriteString(":SOUR:POW -26 dBm");
             }
             else if(num == 2)
             {
                 instrumentSig2.WriteString(":SOUR:FREQ  " + fmain.dt.Rows[0]["F2"] + "MHz");
                 instrumentSig2.WriteString(":SOUR:POW -26 dBm");
             }
         }

         public void SetRFOn(int num)
         {
             if (num == 1)
             {
                 instrumentSig1.WriteString(":OUTP:STAT 1");
             }
             else if (num == 2)
             {
                 instrumentSig2.WriteString(":OUTP:STAT 1");
             }
         }
         public void SetRFOff(int num)
         {
             if (num == 1)
             {
                 instrumentSig1.WriteString(":OUTP:STAT 0");
             }
             else if (num == 2)
             {
                 instrumentSig2.WriteString(":OUTP:STAT 0");
             }
         }

         public double PowerGet(int num)
         {
             double sigCurrentPower=-50;
             if (num == 1)
             {
                 instrumentSig1.WriteString(":POWer:LEVel?");
                 sigCurrentPower= instrumentSig1.ReadNumber();
             }
             else if (num == 2)
             {
                 instrumentSig2.WriteString(":POWer:LEVel?");
                 sigCurrentPower= instrumentSig2.ReadNumber();
             }
             return sigCurrentPower;
         }
        /// <summary>
        /// 校准过程：调整信号源功率
        /// </summary>
        /// <param name="num">信号源名字</param>
        /// <param name="UpDown">上调还是下调</param>
        /// <param name="stepValue">校准具体值</param>
         public void PowerUpOrDown(int num,bool UpDown, double stepValue)
         {
             if (num == 1)
             {
                 instrumentSig1.WriteString (":POWer?");
                 double currentPower = instrumentSig1.ReadNumber();
                 if (UpDown == true)
                 {
                     instrumentSig1.WriteString(":POWer " + (currentPower + stepValue)  + "DBM");
                 }
                 else
                 {
                     instrumentSig1.WriteString(":POWer " + (currentPower - stepValue) + "DBM");
                 }
             }
             else if (num == 2)
             {
                 instrumentSig2.WriteString(":POWer?");
                 double currentPower = instrumentSig2.ReadNumber();
                 if (UpDown == true)
                 {
                     instrumentSig2.WriteString(":POWer " + (currentPower + stepValue) + "DBM");
                 }
                 else
                 {
                     instrumentSig2.WriteString(":POWer " + (currentPower - stepValue) + "DBM");
                 }
             }
         }

         public void SetFreqPower(int num,Frm_Main fmain)
         {
             if (num == 1)
             {
                 instrumentSig1.WriteString(":SOUR:FREQ " + fmain.dataGridView1.Rows[0].Cells["F1"].Value + "MHz");
                 instrumentSig1.WriteString(":SOUR:POW " + '-' + fmain.dataGridView1.Rows[0].Cells["P1"].Value + " dBm");
             }
             else if (num == 2)
             {
                 instrumentSig2.WriteString(":SOUR:FREQ  " + fmain.dataGridView1.Rows[0].Cells["F2"].Value + "MHz");
                 instrumentSig2.WriteString(":SOUR:POW " + '-' + fmain.dataGridView1.Rows[0].Cells["P2"].Value + " dBm");
             }
         }
    }
}
